
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
 <%@ page import="cart.CartItem" %>  
 <%@ page import="java.sql.*" %>
 <%@ page import="javax.sql.rowset.WebRowSet" %> 
<!DOCTYPE html> 
	<html>  
	<head>  
	    <title>Cart</title>  
	    <style>  
        body {  
            font-family: Arial, sans-serif;  
            margin: 20px;  
        }  
        h1 {  
            color: #333;  
            text-align: center;  
        }  
        p {  
            margin-bottom: 10px;  
        }  
        .product-info {  
            border: 1px solid #ccc;  
            padding: 10px;  
        }  
        a {  
            text-decoration: none;  
            color: #007BFF;  
        }  
    </style> 
	</head>  
	<body>  
	<%
request.setCharacterEncoding("utf-8");
String goodsName[]=request.getParameterValues("choice");
if(goodsName!=null) {
for(int k=0;k<goodsName.length;k++){
session.removeAttribute(goodsName[k]);
}
}
%>
	    <h1>购物车</h1>  
	    <%  
	        CartItem cartItem = (CartItem) request.getSession().getAttribute("cartItem");  
	        if (cartItem != null) {  
	    %>  
	    <p>Product Name: <%= cartItem.getProductName() %></p>  
	    <p>Quantity: <%= cartItem.getQuantity() %></p>  
	    <%  
	        } else {  
	    %>  
	
<P>选购列表<br></P>
	    <p>到底了~</p> 
	    <p>如果需要，请 <a href="menu.jsp">继续点餐</a></p>

</FORM>
	    
	    <%  
	        }  
	    %> 
	    <%
//获取原始记录数据
String id = request.getParameter("id");
//查询原始数据，连接DB,SELECT ->变量
String foodid = "",foodname = "",foodprice = "",deskid = "";
Class.forName("org.mariadb.jdbc.Driver");
//建立与数据库的连接
Connection connection = DriverManager.getConnection("jdbc:mariadb://10.220.140.102:3366/studb14","stu14", "jyw745DJ");
//创建语句集
Statement stmt = connection.createStatement();
System.out.println("select * from shoppingcart where foodid="+id);
ResultSet rs = stmt.executeQuery("select * from shoppingcart where foodid="+id);
if(rs.next()){
	//rs-->变量
	foodid = rs.getString("foodid");
	foodname = rs.getString("foodname");
	foodprice = rs.getString("foodprice");
	deskid = rs.getString("deskid");
}
rs.close();
stmt.close();
connection.close();
%> 
	    
	</body>  
	</html>
=======
<head>  
    <title>菜单</title>  
    <!-- 引入CSS和JS文件（如果需要） --> 
     
</head>  
<body>  

<body style="text-align: center;margin: auto;">

<div class="nav">
 
		<header class="mui-bar mui-bar-nav color">
		    <h1 class="mui-title color">订单详情</h1>
		</header>
 <%
   String restaurant_id = (String) session.getAttribute("restaurant_id");
   String table_id = (String) session.getAttribute("table_id");
   String account = (String) session.getAttribute("account");
   String pay = (String) session.getAttribute("pay");
   
    if (table_id != null) {
        %>
        <h4>当前餐馆号： <%= restaurant_id %></h4>
        <h4>您的桌号是： <%= table_id %></h4>
        <h4>当前用户： <%= account %></h4>
        
        <% 
        // 这里可以添加更多的点餐逻辑
    } else {
        %>
        <p>请扫描桌子上的二维码开始点餐。</p >
        <%
    }
    %>
		
	   <div class="container"> 

  <table width="80%" class="table" id="tablevalue">  
   <%  
    double totalPrice = 0; // 用于存储总价
    String selectSql = "SELECT * FROM cart WHERE table_id=? AND restaurant_id=? AND pay=?";
    try (Connection conn = com.myweb.DBTools.getConn();
         PreparedStatement pstmt = conn.prepareStatement(selectSql)) {
        pstmt.setString(1, table_id);
        pstmt.setString(2, restaurant_id);
        pstmt.setString(3, "未支付"); // 使用参数化查询来避免硬编码
        ResultSet rs = pstmt.executeQuery();

        while (rs.next()) {
            // 计算总价
            double price = rs.getDouble("price");
            int amount = rs.getInt("amount");
            totalPrice += price * amount;
            // 省略数据展示代码
        }

        // 显示总价
        out.println("<tr><td colspan=\"5\" style=\"text-align:right;\">总价：" + totalPrice + "</td></tr>");
    } catch (java.sql.SQLException e) {
        // 记录异常到日志
        // 给用户一个友好的错误提示
        out.println("<p>查询数据库时发生错误，请稍后再试。</p>");
        e.printStackTrace();
    }
%>
    </table>
<form action="payment" method="post">
            <input type="hidden" name="table_id" value="<%= table_id %>">
            <input type="hidden" name="restaurant_id" value="<%= restaurant_id %>">
            <input type="submit" value="结算">
        </form>
    </div>
</div>
 

  
</body>  
</html>
